package images;

import game.Configurations;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.jbox2d.dynamics.FixtureDef;
import org.json.simple.JSONArray;
import org.json.simple.JSONValue;
import static org.lwjgl.opengl.GL11.*;
import org.newdawn.slick.opengl.Texture;

/**
 *
 * @author Luan
 */
public class ObjectLoader {
    
    private static Texture imgStars, imgPlanets, imgObjects;
    private static ImageInfo[] stars, planets, objects, explosion, implosion, lasers;
    private static ImageInfo exp_par;
    private static FixtureDef[] bObjects;
    
    public static Texture BACK, HUD, HUD_BARS;
    
    public static final String STARS_FILE = "stars.png", PLANETS_FILE = "planets.png", OBJECTS_FILES = "objects.png";
    public static final int STAR_SIZE = 32, PLANET_SIZE = 32;
    
    public static void load() throws IOException {
        imgStars = ImageManager.getTexture(STARS_FILE);
        imgPlanets = null;//Game.getTexture(PLANETS_FILE);
        imgObjects = ImageManager.getTexture(OBJECTS_FILES);
        
        BACK = ImageManager.getTexture("back.png");
        HUD = ImageManager.getTexture("hud.png");
        HUD_BARS = ImageManager.getTexture("bars.png");
        
        float px, py;
        int col;
        
        stars = new ImageInfo [7];
        px = imgStars.getWidth()/imgStars.getImageWidth();
        py = imgStars.getHeight()/imgStars.getImageHeight();
        col = imgStars.getTextureWidth() / STAR_SIZE;
        for (int i = 0; i < stars.length; i++)
            stars[i] = new ImageInfo(i%col * STAR_SIZE, i/col * STAR_SIZE, STAR_SIZE, STAR_SIZE, 0, px, py);
        
        if (imgPlanets != null) {
            planets = new ImageInfo [0]; //TODO add planets
            px = imgPlanets.getWidth()/imgPlanets.getImageWidth();
            py = imgPlanets.getHeight()/imgPlanets.getImageHeight();
            col = imgPlanets.getTextureWidth() / PLANET_SIZE;
            for (int i = 0; i < planets.length; i++)
                planets[i] = new ImageInfo(i%col * PLANET_SIZE, i/col * PLANET_SIZE, PLANET_SIZE, PLANET_SIZE, 0, px, py);
        }
        
        if (imgObjects != null) {
            px = imgObjects.getWidth()/imgObjects.getImageWidth();
            py = imgObjects.getHeight()/imgObjects.getImageHeight();
            
            explosion = new ImageInfo[10];
            for (int i = 0; i < explosion.length; i++)
                explosion[i] = new ImageInfo(i*64, 48, 64, 64, 0, px, py);
            exp_par = new ImageInfo(195, 19, 2, 4, 0, px, py);
            
            implosion = new ImageInfo[11];
            for (int i = 0; i < implosion.length; i++)
                implosion[i] = new ImageInfo(i*64, 112, 64, 64, 0, px, py);
            
            lasers = new ImageInfo[3];
            lasers[0] = new ImageInfo(205, 0, 3, 5, 0, px, py);
            lasers[1] = new ImageInfo(208, 0, 3, 5, 0, px, py);
            lasers[2] = new ImageInfo(211, 0, 3, 5, 0, px, py);
            
            //read data from json file
            JSONArray el = (JSONArray) JSONValue.parse(new String(Files.readAllBytes(Paths.get(ImageManager.getPath() + "objects.json")))),
                      bs = (JSONArray) JSONValue.parse(new String(Files.readAllBytes(Paths.get(ImageManager.getPath() + "objects.bounds.json"))));
            objects = new ImageInfo[el.size()];
            bObjects = new FixtureDef[objects.length];
            for (int i = 0; i < objects.length; i++) {
                objects[i] = ImageManager.getImage(el, i, px, py);
                objects[i].setAngle(objects[i].getAngle() * 90);
                bObjects[i] = ImageManager.getFixture(bs, i);
            }
        }
    }
    
    public static FixtureDef getFixture(int code) {
        return bObjects[code];
    }
    
    public static void bindObjects() {
        imgObjects.bind();
    }
    
    public static void drawStar(int cod, int x, int y, int w, int h) {
        imgStars.bind();
        stars[cod].draw(x, y, w, h);
    }
    
    public static void drawImplosion(int cod, int x, int y, int s) {
        implosion[cod].draw(x, y, s, s);        
    }
    
    public static void drawExplosion(int cod, int x, int y, int s) {
        explosion[cod].draw(x, y, s, s);        
    }
    
    public static void drawExpParticle(int x, int y, float ang) {
        exp_par.draw(x, y, ang);
    }
    
    public static void drawLaser(int cod, int x, int y, int w, int h) {
        lasers[cod].draw(x, y, w, h);
    }
    
    public static void drawPlanet(int cod, int x, int y, int w, int h) {
        imgPlanets.bind();
        planets[cod].draw(x, y, w, h);
    }
    
    public static void drawObject(int cod, int x, int y) {
        drawObject(cod, x, y, 0);
    }
    
    public static void drawObject(int cod, int x, int y, int w, int h) {
        objects[cod].draw(x - w/2, y - h/2, w, h);
    }
    
    public static void drawObject(int cod, int x, int y, float ang) {
        objects[cod].drawAngle(x - objects[cod].getWidth()/2, y - objects[cod].getHeight()/2, ang);
    }
    
    public static void drawObject(int cod, int x, int y, int w, int h, float ang) {
        objects[cod].draw(x - objects[cod].getWidth()/2, y - objects[cod].getHeight()/2, w, h, ang);
    }
    
    public static void drawBack(int x, int y) {
        ImageInfo.draw(x, y, BACK);
    }
    
    public static int getObjectWidth(int obj) {
        return objects[obj].getWidth();
    }
    
    public static int getObjectHeight(int obj) {
        return objects[obj].getHeight();
    }
    
    //HUD
    public static void drawHUD() {
        ImageInfo.draw(0, Configurations.GAME_HEIGHT, HUD);
    }
    
    public static void drawBar(int tipo, int x, int y, int ax, int mx, int h) {
        //glDisable(GL_BLEND);
        HUD_BARS.bind();
        float tw = HUD_BARS.getWidth(), th = HUD_BARS.getHeight();
        float t1 = tw * (4 * tipo + 2) / 12, t2 = tw * (4 * tipo + 1) / 12;

        if (ax != 0) {
            glBegin(GL_QUADS);
            {
                glTexCoord2f(t1, 0);
                glVertex2i(x, y);
                glTexCoord2f(t1, 0);
                glVertex2i(x + ax, y);
                glTexCoord2f(t1, th);
                glVertex2i(x + ax, y + h);
                glTexCoord2f(t1, th);
                glVertex2i(x, y + h);
            }
            glEnd();
        }

        glBegin(GL_QUADS);
        {
            glTexCoord2f(t2, 0);
            glVertex2i(x + ax, y);
            glTexCoord2f(t2, 0);
            glVertex2i(x + mx, y);
            glTexCoord2f(t2, th);
            glVertex2i(x + mx, y + h);
            glTexCoord2f(t2, th);
            glVertex2i(x + ax, y + h);
        }
        glEnd();

        //glEnable(GL_BLEND);
    }
}
